﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using PPI.Entities;
using PPI.Entities.Interface;
using PPI.Mobile;
using VMind.Global;
using VMind.Global;
using PPI.Data;


namespace PPI.ServiceLogic
{
    public class LVendor_Visit_XMissionStructures_Image :IMobileTable
    {
        public static List<EVendor_Visit_XMissionStructures_Image> GetServerImages(string connectionKey)
        {
            DataTable _ImagesDatatable = DVendor_Visit_XMissionStructures_Image.GetServerImages(0, connectionKey).Tables[0];


            EVendor_Visit_XMissionStructures_Image _Image;
            List<EVendor_Visit_XMissionStructures_Image> _Images = new List<EVendor_Visit_XMissionStructures_Image>();
            foreach (DataRow _ImageData in _ImagesDatatable.Rows)
            {
                #region Images
                _Image = new EVendor_Visit_XMissionStructures_Image();
                _Image.Id = VMind.Global.ConvertHelper.ToInt32(_ImageData["Vendor_Visit_XMissionStructures_ImageId"]);
                _Image.Latitude = ConvertHelper.Todouble(_ImageData["Latitude"]);
                _Image.Longitude = ConvertHelper.Todouble(_ImageData["Longitude"]);
                _Image.Altitude = ConvertHelper.Todouble(_ImageData["Altitude"]);
                _Image.SatelliteCount = ConvertHelper.Toint(_ImageData["SatelliteCount"]);
                _Image.SatelliteTime = ConvertHelper.ToDateTime(_ImageData["SatelliteTime"]);
                _Image.SolutionAvailable = ConvertHelper.Tobool(_ImageData["SolutionAvailable"]);
                _Image.FileName = ConvertHelper.Tostring(_ImageData["Filename"]);
                _Image.Asset = new EVendor_Visit_XMissionStructure(ConvertHelper.ToInt32(_ImageData["Vendor_Visit_XMissionStructureFK"]));
                _Image.Pole = new EVendor_Visit_XMissionStructures_Pole(ConvertHelper.ToInt32(_ImageData["Vendor_Visit_XMissionStructures_PoleFK"]));
                #endregion Images
                if (!_Images.Contains(_Image))
                    _Images.Add(_Image);
            }
            return _Images;
        }

        public static void AssignImagesToStructure(EBaseAsset structure, List<EVendor_Visit_XMissionStructures_Image> images)
        {
            foreach (EBaseAssetImage i in images)
            {
                if (i.Asset.Id == structure.Id)
                {
                    if (!structure.Images.Contains(i))
                    {
                        structure.Images.Add(i);
                    }
                }
            }
        }

        public static void AssignImagesToPole(EBasePole pole, List<EVendor_Visit_XMissionStructures_Image> images)
        {
            foreach (EBaseAssetImage i in images)
            {
                if (i.Pole.Id != Int32.MinValue)
                {
                    if (pole.Id == i.Pole.Id)
                    {
                        if (!pole.Images.Contains(i))
                        {
                            pole.Images.Add(i);
                            i.Pole = pole;
                        }
                        
                    }
                }
            }
        }



        #region CRUD

        public static int Save(int imageId,int vendor_Visit_XMissionStructureFK, bool solutionAvaible, string fileName, double latitude, double altitude, double longitude, DateTime satelliteTime, int satelliteCount, double direction, int vendor_Visit_XMissionStructures_PoleID, string connectionKey)
        {
            if (imageId == Int32.MinValue)
                return Insert(vendor_Visit_XMissionStructureFK, solutionAvaible, fileName, latitude, altitude, longitude, satelliteTime, satelliteCount, direction, vendor_Visit_XMissionStructures_PoleID, connectionKey);
            else
            {
                Update(imageId,solutionAvaible,fileName,latitude,altitude,longitude,satelliteTime,satelliteCount,direction,vendor_Visit_XMissionStructures_PoleID,connectionKey);
                return imageId;
            }

            
        }

        private static int Insert(int vendor_Visit_XMissionStructureFK, bool solutionAvaible, string fileName, double latitude, double altitude, double longitude, DateTime satelliteTime, int satelliteCount, double direction, int vendor_Visit_XMissionStructures_PoleID, string connectionKey)
        {

            Int32 newImageId = DVendor_Visit_XMissionStructures_Image.Insert(vendor_Visit_XMissionStructureFK, solutionAvaible,
                                                              fileName, latitude, altitude, longitude, satelliteTime,
                                                              satelliteCount, direction,
                                                              vendor_Visit_XMissionStructures_PoleID, connectionKey);

            string FileName = vendor_Visit_XMissionStructureFK + "_" + newImageId + ".jpg";
            DVendor_Visit_XMissionStructures_Image.UpdateFileName(newImageId,vendor_Visit_XMissionStructureFK,FileName,connectionKey);

            return newImageId;
                
        }

        private static void Update(Int32 imageId, bool solutionAvaible, string fileName, double latitude, double altitude, double longitude, DateTime satelliteTime, int satelliteCount, double direction, int vendor_Visit_XMissionStructures_PoleID, string connectionKey)
        {
            DVendor_Visit_XMissionStructures_Image.UpdatePoleReference(solutionAvaible, latitude, altitude, longitude, satelliteTime, satelliteCount, direction, vendor_Visit_XMissionStructures_PoleID, imageId, connectionKey);
        }

        public static void Delete(Int32 imageId, string connectionKey)
        {
            DVendor_Visit_XMissionStructures_Image.Delete(imageId,connectionKey);
        }

        #endregion CRUD

        public List<EMobileQuery> CreateInsertMobileQuery(string connectionKey)
        {
            throw new NotImplementedException();
        }
    }
}
